﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tomahawk.Runtime.Logic.UI;
using Tomahawk.Runtime;
using Tomahawk.Runtime.Logic;

namespace UIScenes.Logic
{
    /// <summary>
    /// If you need to add code to a form, you simply have to derive from XUIScene
    /// </summary>
    public class MyScene : XUIScene
    {
        /// <summary>
        /// Initialization
        /// </summary>
        protected override void OnInitialize()
        {
            base.OnInitialize();

            //get controls
            XUIButton button1 = (this.GetControlByName("xuibutton_01") as XUIButton);
            XUIButton button2 = (this.GetControlByName("xuibutton_02") as XUIButton);
            XUIButton button3 = (this.GetControlByName("xuibutton_03") as XUIButton);
            XUIButton button4 = (this.GetControlByName("xuibutton_04") as XUIButton);
            XUIButton button5 = (this.GetControlByName("xuibutton_05") as XUIButton);

            //subscribe to button press event
            button1.ButtonPressed += new ControlUserActivation(MyScene_ButtonPressed);
            button2.ButtonPressed += new ControlUserActivation(MyScene_ButtonPressed);
            button3.ButtonPressed += new ControlUserActivation(MyScene_ButtonPressed);
            button4.ButtonPressed += new ControlUserActivation(MyScene_ButtonPressed);
            button5.ButtonPressed += new ControlUserActivation(MyScene_ButtonPressed);

            //add a new button dinamically
            XUIButton dyn1 = (XUIButton)XWorld.Instance.CreateObject(false, typeof(XUIButton), this, "", "");
            dyn1.UIName = "dyn1";
            dyn1.ImagePressed.textureAsset = @"Examples.UIScenes.Content\ButtonPressed";
            dyn1.ImageSelected.textureAsset = @"Examples.UIScenes.Content\ButtonSelected";
            dyn1.ImageUnselected.textureAsset = @"Examples.UIScenes.Content\ButtonUnselected";
            dyn1.Position.Left = 100;
            dyn1.Position.Top = 300;
            dyn1.Position.Width = 60;
            dyn1.Position.Height = 20;
            dyn1.Position.Docking = XUIPlaceableControl.DockingModes.TopLeft;
            dyn1.TextComponent.Text = "DynButton 1";
            this.AddChildControl(dyn1);
            dyn1.Initialize();

            //add a new button dinamically
            XUIButton dyn2 = (XUIButton)XWorld.Instance.CreateObject(false, typeof(XUIButton), this, "", "");
            dyn2.UIName = "dyn2";
            dyn2.ImagePressed.textureAsset = @"Examples.UIScenes.Content\ButtonPressed";
            dyn2.ImageSelected.textureAsset = @"Examples.UIScenes.Content\ButtonSelected";
            dyn2.ImageUnselected.textureAsset = @"Examples.UIScenes.Content\ButtonUnselected";
            dyn2.Position.Left = 200;
            dyn2.Position.Top = 300;
            dyn2.Position.Width = 60;
            dyn2.Position.Height = 20;
            dyn2.Position.Docking = XUIPlaceableControl.DockingModes.TopLeft;
            dyn2.TextComponent.Text = "DynButton 2";
            this.AddChildControl(dyn2);
            dyn2.Initialize();

            //link focus referenced between already existing controls, 
            //and the dynamically created ones

            dyn1.FocusReferences.Left = button5.UIName;
            dyn1.FocusReferences.Up = button5.UIName;
            dyn1.FocusReferences.Right = "dyn2";
            dyn1.FocusReferences.Down = button4.UIName;

            dyn2.FocusReferences.Left = "dyn1";
            dyn2.FocusReferences.Up = button5.UIName;
            dyn2.FocusReferences.Right = "dyn1";
            dyn2.FocusReferences.Down = button4.UIName;

            button4.FocusReferences.Up = dyn1.UIName;
            button5.FocusReferences.Down = dyn1.UIName;

            //focus a control arbitrarily
            dyn1.Focus();
        }

        /// <summary>
        /// Handle the button pressed event!
        /// </summary>
        void MyScene_ButtonPressed(XUIControl sourceControl)
        {
            Engine.Instance.DebugConsole.WriteLine(
                String.Format("Button {0} has been pressed!", sourceControl.UIName));
        }

    }

}
